package datacollection;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import bot.Util;

public class ExtraFieldsAdder {
	
	private static File output = new File("c:\\Users\\Jamison\\Documents\\PopulationInfo\\MarylandData.txt");
	private static File estimates = new File("c:\\Users\\Jamison\\Documents\\PopulationInfo\\MarylandEstimates.txt");
	
	//cities
	public static void main(String[] args){
		
		try{
			List<String> dataLines = new ArrayList<String>();
			BufferedReader reader = new BufferedReader(new FileReader(output));
			String line = null;
			while ((line = reader.readLine()) != null){
				dataLines.add(line);
			}
			reader.close();
			reader = new BufferedReader(new FileReader(estimates));
			List<String> estimateLines = new ArrayList<String>();
			line = null;
			while ((line = reader.readLine()) != null){
				estimateLines.add(line);
			}
			
			for (String entry: estimateLines){
				String values[] = entry.split(",");
				for (int i = 0; i < dataLines.size(); i++){
					String placeSplit[] = dataLines.get(i).split(" ;&: ");
					if (values[3].equals(placeSplit[DataFields.NAME] + " " + placeSplit[DataFields.TYPE]) && 
							values[5].equals(Util.removeCommas(placeSplit[DataFields.TOTAL_POPULATION]))){
						placeSplit[DataFields.POPULATION_ESTIMATE_2011] = values[8];
						placeSplit[DataFields.SUMLEV] = values[0];
						placeSplit[DataFields.STATE] = values[1];
						placeSplit[DataFields.PLACE] = values[2];
						StringBuilder builder = new StringBuilder(1000);
						for (int j = 0; j < placeSplit.length; j++){
							if (j == placeSplit.length - 1){
								builder.append(placeSplit[j]);
							}
							else{
								builder.append(placeSplit[j]);
								builder.append(" ;&: ");
							}
						}
						dataLines.set(i, builder.toString());
						break;
					}
				}
			}
			
			BufferedWriter writer = new BufferedWriter(new FileWriter(output));
			for (String toAdd: dataLines){
				writer.write(toAdd);
				writer.write("\n");
			}
			writer.close();
		} 
		catch (FileNotFoundException e){
			e.printStackTrace();
		} 
		catch (IOException e){
			e.printStackTrace();
		}
	}
	
//	//minor civil divisions
//	public static void main(String[] args){
//		try{
//			List<String> dataLines = new ArrayList<String>();
//			BufferedReader reader = new BufferedReader(new FileReader(output));
//			String line = null;
//			while ((line = reader.readLine()) != null){
//				dataLines.add(line);
//			}
//			reader.close();
//			reader = new BufferedReader(new FileReader(estimates));
//			List<String> estimateLines = new ArrayList<String>();
//			line = null;
//			while ((line = reader.readLine()) != null){
//				//make sure its not a county
//				if (line.substring(0, 3).equals("...")){
//					String test[] = line.split(",");
//					if (test[0].substring(test[0].length() - 4, test[0].length()).equals("town")){
//						estimateLines.add(line);
//					}
//				}
//			}
//
//			for (String entry: estimateLines){
//				boolean quotes = true;
//				String values[] = entry.split(",\"");
//				if (values.length == 1){
//					quotes = false;
//					values = entry.split(",");
//				}
//				for (int i = 0; i < dataLines.size(); i++){
//					String placeSplit[] = dataLines.get(i).split(" ;&: ");
//					if (values[0].substring(3, values[0].length()).equals(placeSplit[DataFields.NAME] + " " + placeSplit[DataFields.TYPE])){
//						if ((quotes && Util.removeCommas(values[1].substring(0, values[1].length() - 1)).equals(Util.removeCommas(placeSplit[DataFields.TOTAL_POPULATION]))) ||
//								(!quotes && values[1].equals(Util.removeCommas(placeSplit[DataFields.TOTAL_POPULATION])))){
//							
//							String estimate = null;
//							try{
//							if (quotes){
//								estimate = values[4].substring(0, values[4].length() - 5);
//								estimate = Util.removeCommas(estimate);
//							}
//							else{
//								estimate = values[4];
//							}
//							
//							}
//							catch (ArrayIndexOutOfBoundsException e){
//								System.out.println("ERROR~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " + placeSplit[DataFields.NAME]);
//								break;
//							}
//							placeSplit[DataFields.POPULATION_ESTIMATE_2011] = estimate;
////							placeSplit[DataFields.SUMLEV] = values[0];
////							placeSplit[DataFields.STATE] = values[1];
////							placeSplit[DataFields.PLACE] = values[2];
//							StringBuilder builder = new StringBuilder(1000);
//							for (int j = 0; j < placeSplit.length; j++){
//								if (j == placeSplit.length - 1){
//									builder.append(placeSplit[j]);
//								}
//								else{
//									builder.append(placeSplit[j]);
//									builder.append(" ;&: ");
//								}
//							}
//							dataLines.set(i, builder.toString());
//							break;
//						}
//					}
//				}
//			}
//
//			BufferedWriter writer = new BufferedWriter(new FileWriter(output));
//			for (String toAdd: dataLines){
//				writer.write(toAdd);
//				writer.write("\n");
//			}
//			writer.close();
//		} 
//		catch (FileNotFoundException e){
//			e.printStackTrace();
//		} 
//		catch (IOException e){
//			e.printStackTrace();
//		}
//	}
}
